Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)


問題描述

Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)

我正在嘗試使用 django‑pytestwagtail_factories 在 Wagtail 中設置一個簡單的測試,以測試用戶編輯 Pages 他們的能力自己的。但是當我嘗試獲取編輯網址時,我一直在重定向。(對於我這裡的示例,我使用超級用戶固定裝置不必處理權限。)

# test_views.py

def test_user_can_edit_owned_pages(client, superuser):
    parent_page = MyPageIndexPageFactory()
    my_page = MyPageFactory(owner=superuser, parent=parent_page)

    edit_url = reverse("register_mypage_modeladmin_edit", args=[my_page.pk])

    client.force_login(superuser)
    response = client.get(edit_url)

    # to capture in pytests output  
    print(
        superuser.is_superuser, 
        my_page.id, 
        response
    )    

    assert response.status_code == 200

assert 失敗,儘管捕獲的輸出顯示 Page get 在 url 中使用正確的 ID 創建 ‑ 但我仍然在重定向。

True 4778 <HttpResponseRedirect status_code=302, "text/html; charset=utf‑8", url="/admin/pages/4778/edit/?next=/admin/my_app/mypage/">

當我獲得模型列表視圖的索引 url 時,一切都很好。

    [...]
    index_url = reverse("register_mypage_modeladmin_index")
    response = client.get(index_url)
    qs = response.context[0]["object_list"]
    print(qs)

捕獲的輸出:]>

我懷疑這(仍然)是一個權限問題,即使我使用的是 superuser。如何在 Wagtail 中設置這樣的簡單測試?


參考解法

方法 1:

The URL route you're accessing (register_mypage_modeladmin_edit) is part of the modeladmin module, which does indeed redirect to the standard page edit view when you use it on a page model:

For Page models, the system directs to Wagtail’s existing add and edit views, and returns you back to the correct list page, for a seamless experience.

So, what you're seeing is Modeladmin working as designed. Perhaps you intended to run this test against the standard page edit view instead? If so, the URL route you want is wagtailadmin_pages:edit.

(by Jean Zombiegasman)

參考文件

  1. Wagtail: How to setup up unittest for simple page edit? (CC BY‑SA 2.5/3.0/4.0)

#wagtail #Python #Django






相關問題

Wagtail Cms 是否支持 Google 登錄和用戶登錄添加會話 (Does Wagtail Cms support Google login and user login add session to)

Wagtail Django-form編輯現有對象 (Wagtail Django-form edit existing object)

如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)

django.db.utils.OperationalError:外鍵不匹配 - “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user")

如何使用 Wagtail 鉤子在 Wagtail 中生成自定義鏈接 (How to generate a custom link in Wagtail using Wagtail hooks)

Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)

如何修復錯誤“str”對像沒有屬性“relative_url” (How to fix error 'str' object has no attribute 'relative_url')

如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)

如何用外鍵鏈接兩種形式(wagtail 形式和 django 形式)? (How to link two forms (wagtail form and django form) with a foreign key?)

為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:<h2>嘗試 post.content|richtext</h2> (Why is RichText not working in wagtail admin for posts? This is the type of thing that happens: <h2>Trying post.content|richtext</h2>)

Windows 10 上 wagtail 的客戶端文件夾在哪裡 (Where is the client folder of wagtail on windows 10)

過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)







留言討論